package com.didispace;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@RefreshScope//添加该配置，refresh后，只要在重新调用这个类的时候才会重新注入新的实例
@Component
@Lazy(true)
public class MyBean2 {

	@Value("${from}")
	private String from;
	
	
	public MyBean2() {
		System.out.println("mybean2 constructor");
	}
	@PostConstruct
	public void init(){
		System.out.println("initial Mybean2 by @PostConstruct...");
	}
	
	public void doing (){
		System.out.println(" doing ..."+ from);
	}
	
	@Autowired
	ApplicationContext context1;
	
	@Autowired
	ConfigurableApplicationContext context;
	
	/**
	 * 若添加该监听，该bean会在context更新完成后生成，@Lazy(true)失效， 
	 * 相当于更新完成后的首次调用该bean
	 */
//	@EventListener(RefreshScopeRefreshedEvent.class)
//    public void refreshBean(RefreshScopeRefreshedEvent event){
//    	System.out.println("event happened:"+event);
//    }
}
